﻿using LeaRun.Application.Entity.AppManage;
using LeaRun.Application.IService.AppManage;
using LeaRun.Data.Repository;
using System;
using System.Collections.Generic;

namespace LeaRun.Application.Service.AppManage
{
    /// <summary>
    /// App项目模板服务
    /// </summary>
    public class App_ProjectService : RepositoryFactory, App_ProjectIService
    {
        /// <summary>
        /// 获取Project
        /// </summary>
        /// <param name="keyValue">key关键字</param>
        /// <returns></returns>
        public App_ProjectEntity GetEntity(string keyValue)
        {
            return base.BaseRepository().FindEntity<App_ProjectEntity>(keyValue);
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="queryJson">查询</param>
        /// <returns></returns>
        public IEnumerable<App_ProjectEntity> GetList(string queryJson)
        {
            return base.BaseRepository().FindList<App_ProjectEntity>("select * from App_Project order by F_CreateDate desc");
        }
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="keyValue">关键字</param>
        public void RemoveForm(string keyValue)
        {
            IRepository repository = new RepositoryFactory().BaseRepository().BeginTrans();
            try
            {
                repository.Delete<App_ProjectEntity>(keyValue);
                repository.ExecuteBySql("delete from App_Templates where F_ProjectId='" + keyValue + "'");
                repository.Commit();
            }
            catch (Exception)
            {
                repository.Rollback();
                throw;
            }
        }
        /// <summary>
        /// 保存模板
        /// </summary>
        /// <param name="keyValue">关键字</param>
        /// <param name="entity">项目</param>
        /// <param name="entryList">模板</param>
        public void SaveForm(string keyValue, App_ProjectEntity entity, List<App_TemplatesEntity> entryList)
        {
            IRepository db = base.BaseRepository().BeginTrans();
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    db.ExecuteBySql("delete from App_Templates where F_ProjectId='" + keyValue + "'");
                    db.ExecuteBySql(string.Concat(new object[]
                    {
                        "update [App_Project] set F_Name='",entity.F_Name,
                        "',F_Icon='",entity.F_Icon,
                        "',F_IsTabed=",entity.F_IsTabed,
                        ",F_Description='",entity.F_Description,
                        "' where F_Id='",keyValue,"'"
                    }));
                    using (List<App_TemplatesEntity>.Enumerator enumerator = entryList.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            App_TemplatesEntity current = enumerator.Current;
                            current.Create();
                            current.F_ProjectId = keyValue;
                            db.ExecuteBySql(string.Concat(new object[]
                            {
                                "insert into App_Templates([F_Id],[F_ProjectId],[F_Name],[F_Value],[F_Type],[F_Parent],[F_level],[F_img],[F_Content],[F_CreateDate],[F_CreateUserId],[F_CreateUserName]) values('",
                                current.F_Id,"','",
                                current.F_ProjectId,"','",
                                current.F_Name,"','",
                                current.F_Value,"','",
                                current.F_Type,"','",
                                current.F_Parent,"',",
                                current.F_level,",'",
                                current.F_img,"','",
                                current.F_Content,"','",
                                current.F_CreateDate,"','",
                                current.F_CreateUserId,"','",
                                current.F_CreateUserName,"')"
                            }));
                        }

                    }
                }
                else
                {
                    entity.Create();
                    entity.F_Templates = null;
                    db.Insert<App_ProjectEntity>(entity);
                    foreach (App_TemplatesEntity current2 in entryList)
                    {
                        current2.Create();
                        current2.F_ProjectId = entity.F_Id;
                        db.ExecuteBySql(string.Concat(new object[]
                    {
                        "insert into App_Templates([F_Id],[F_ProjectId],[F_Name],[F_Value],[F_Type],[F_Parent],[F_level],[F_img],[F_Content],[F_CreateDate],[F_CreateUserId],[F_CreateUserName]) values('",
                        current2.F_Id,"','",
                        current2.F_ProjectId,"','",
                        current2.F_Name,"','",
                        current2.F_Value,"','",
                        current2.F_Type,"','",
                        current2.F_Parent,"',",
                        current2.F_level,",'",
                        current2.F_img,"','",
                        current2.F_Content,"','",
                        current2.F_CreateDate,"','",
                        current2.F_CreateUserId,"','",
                        current2.F_CreateUserName,"')"
                    }));
                    }
                }
                db.Commit();
            }
            catch (Exception)
            {
                db.Rollback();
                throw;
            }
        }
    }
}
